package net.sf.midollo.net.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import net.sf.midollo.io.IOStream;
import net.sf.midollo.io.IOStreamConfig;
import net.sf.midollo.net.NetUtils;

public class SocketIOStream implements IOStreamConfig {

  protected final Socket socket;

  public SocketIOStream(Socket socket) {
    this.socket = socket;
  }

  public void config(IOStream ioSrc) throws IOException {
    if (ioSrc instanceof SocketIOStream) {
      SocketIOStream ioSocketSrc = (SocketIOStream)ioSrc;
      Socket srcSocket = ioSocketSrc.getSocket();
      NetUtils.copySocketOptions(this.socket, srcSocket);
    }
  }

  public Socket getSocket() {
    return this.socket;
  }

  public InputStream getInput() throws IOException {
    return this.socket.getInputStream();
  }

  public OutputStream getOutput() throws IOException {
    return this.socket.getOutputStream();
  }

  public void close() throws IOException {
    this.socket.close();
  }
}
